$(document).ready(function(){
	$('#formClear').click(function() {
		document.forms['orderForm'].reset();	
	});
	
	$('#formSend').click(function() {
		var isErrors = false;
		
		$('.requiredValue').each(function(){
			if ($(this).attr('value') == '') {
				isErrors = true;
				$(this).qtip({
               		content: '<font color=red>Обязательное поле</font>', // Set the tooltip content to the current corner
               		position: {
                  		corner: {
                     		tooltip: 'leftMiddle', // Use the corner...
                     		target: 'rightMiddle' // ...and opposite corner
                  		}
               		},
               		show: {
                  		when: false, // Don't specify a show event
                  		ready: true // Show the tooltip when ready
               		},
               		hide: {
						when: {
							target: $(this),
							event: 'keypress'
						}
					},
               		style: {
                  		border: {
                     		width: 1,
                     		radius: 10
                  		},
						width: 200,
                  		padding: 1, 
                  		textAlign: 'center',
                  		tip: true, // Give it a speech bubble tip with automatic corner detection
                  		name: 'cream' // Style it according to the preset 'cream' style
               		}
            	});
			}
		});
		
		if (!isErrors) {
			
			$('#formClear').unbind();
			$('#formSend').unbind();
			
			$('#orderForm').ajaxSubmit({
				success: function(){
					showSuccessWindow();
				}
			});
		}	
	});
	
	function showSuccessWindow() {
		$('#qtip-blanket').qtip(
			{
		      content: {
		         title: {
		            text: '<span class="popupTxt">Заказ отправлен</span>'
		         },
		         text: '<span class="popupTxt">' +
				 	   'Спасибо! Ваш заказ принят на рассмотрение!<br />' + 
				 	   'Для того, чтобы вернуться к описанию гостиницы, <a href="/hotel/detailed/id/' + $('#orderHotelId').attr('value') + '/">нажмите сюда</a>.<br />' +
					   'Для того, чтобы вернуться к списку отелей, <a href="/hotel/">нажмите сюда</a>.' +
					   '</span>'
		      },
		      position: {
		         target: $(document.body), // Position it via the document body...
		         corner: 'center' // ...at the center of the viewport
		      },
		      show: {
				  when: false, // Don't specify a show event
				  ready: true // Show the tooltip when ready
		      },
		      hide: false,
		      style: {
		         width: { max: 550 },
		         padding: '14px',
		         border: {
		            width: 9,
		            radius: 9,
		            color: '#666666'
		         },
		         name: 'light'
		      },
		      api: {
		         beforeShow: function()
		         {
		            // Fade in the modal "blanket" using the defined show speed
		            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
		         }
		      }
		   });
	}
	
	$('<div id="qtip-blanket">').css({
		position: 'absolute',
		top: $(document).scrollTop(), // Use document scrollTop so it's on-screen even if the window is scrolled
		left: 0,
		height: $(document).height(), // Span the full document height...
		width: '100%', // ...and full width

		opacity: 0.7, // Make it slightly transparent
		backgroundColor: 'black',
		zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
	}).appendTo(document.body).hide();

	
	var settings = {
		tl: { radius: 14 },
		tr: { radius: 14 },
		bl: { radius: 14 },
		br: { radius: 14 },
		antiAlias: true
	}

	curvyCorners(settings, "#orderFormName");
	curvyCorners(settings, "#orderFormSurename");
	curvyCorners(settings, "#orderFormPhone");
	curvyCorners(settings, "#orderFormEmail");
	curvyCorners(settings, "#orderFormComment");
	curvyCorners(settings, "#requiredFields");
});
